package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.cons.MediaType;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.CourseStatistics;
import cn.ztydata.teachingsystem.heaven.entity.DeptStatistics;
import cn.ztydata.teachingsystem.heaven.entity.SpecStatistics;
import cn.ztydata.teachingsystem.heaven.entity.TeacherStatistics;
import cn.ztydata.teachingsystem.heaven.service.*;
import cn.ztydata.teachingsystem.heaven.web.dto.CourseStatisticsDto;
import cn.ztydata.teachingsystem.heaven.web.dto.DeptStatisticsDto;
import cn.ztydata.teachingsystem.heaven.web.dto.SpecStatisticsDto;
import cn.ztydata.teachingsystem.heaven.web.dto.TeacherStatisticsDto;
import cn.ztydata.teachingsystem.util.StringCodeUtil;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

/**
 * 资源统计控制器类
 * Created by chenxin on 2014/12/1.
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private SpecialtyService specialtyService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private ExerciseService exerciseService;

    /**
     * 系部资源统计列表
     * @return json
     *
     * @author cx
     * @since 2014-12-01
     */
    @RequestMapping(value = "/departList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<DeptStatisticsDto> getDeptList() {
        try {
            //获取系部统计的数据
            List<DeptStatistics> info = departmentService.getStatisticsInfo();

            //映射到dto类输出
            List<DeptStatisticsDto> result = mapper.mapList(info, DeptStatisticsDto.class);

            log.debug("系部资源统计请求成功");
            return new JsonResultList<>(result);
        } catch (ServiceException ex) {
            log.debug("系部资源统计请求失败");
            return new JsonResultList<>(false, ex.getMessage().isEmpty() ? "请求失败" : ex.getMessage());
        }
    }

    /**
     * 专业资源统计列表
     * @return json
     *
     * @author cx
     * @since 2014-12-02
     */
    @RequestMapping(value = "/majorList/{deptNum}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<SpecStatisticsDto> getSpecList(@PathVariable("deptNum") String deptNum) {
        //校验参数
        Assert.hasText(deptNum, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "系部编码"));

        //获取指定系部下的专业统计信息
        try {
            List<SpecStatistics> info = specialtyService.getStatisticsInfo(deptNum);

            //映射到dto类输出
            List<SpecStatisticsDto> result = mapper.mapList(info, SpecStatisticsDto.class);

            log.debug("专业统计信息列表请求成功");
            return new JsonResultList<>(result);
        } catch (ServiceException ex) {
            log.debug("专业统计信息列表请求失败");
            return new JsonResultList<>(false, ex.getMessage().isEmpty() ? "请求失败" : ex.getMessage());
        }
    }

    /**
     * 课程资源统计列表
     * @return json
     *
     * @author cx
     * @since 2014-12-02
     */
    @RequestMapping(value = "/lessonList/{specId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<CourseStatisticsDto> getCourseList(@PathVariable("specId") long specId) {
        //校验参数
        Assert.isTrue(specId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "专业ID"));

        try {
            //获取课程统计的数据
            List<CourseStatistics> info = courseService.getRscTotalForCourse(specId);

            //映射到dto类输出
            List<CourseStatisticsDto> result = mapper.mapList(info, CourseStatisticsDto.class);

            log.debug("课程统计信息请求成功");
            return new JsonResultList<>(result);
        } catch (ServiceException ex) {
            log.debug("课程统计信息请求失败");
            return new JsonResultList<>(false, ex.getMessage().isEmpty() ? "请求失败" : ex.getMessage());
        }
    }

    /**
     * 教师资源统计列表
     * @param deptCode 系部编码
     * @param keyword 搜索关键字
     * @return json
     *
     * @author cx
     * @since 2014-12-03
     */
    @RequestMapping(value = "/teacherList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultList<TeacherStatisticsDto> getTeacherList(@RequestParam("deptCode") String deptCode,
        @RequestParam(value = "keyword", required = false) String keyword) {
        //转码可能的中文参数
        keyword = StringCodeUtil.urlParamTransCode(keyword, StringCodeUtil.UTF8);

        //校验参数
        Assert.hasText(deptCode, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "系部编码"));

        try {
            //获取指定教师的统计信息
            List<TeacherStatistics> info = teacherService.getStatisticsInfo(deptCode, StringUtils.trim(keyword));

            //映射到dto类输出
            List<TeacherStatisticsDto> result = mapper.mapList(info, TeacherStatisticsDto.class);

            log.debug("教师统计信息请求成功");
            return new JsonResultList<>(result);
        } catch (ServiceException ex) {
            log.debug("教师统计信息请求失败");
            return new JsonResultList<>(false, ex.getMessage().isEmpty() ? "请求失败" : ex.getMessage());
        }
    }

    /**
     * 我的资源-统计信息
     *
     * @author chenxin
     * @since 2015/1/10
     */
    @RequestMapping(value = "/mine", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData<Map<String, Object>> getMyRsc() {
        //获取个人课程资源的统计数量
        Map<Integer, Long> courseRscTotal = resourceService.getStatisticsForMine(true);
        //获取个人专业资源的统计数量
        Map<Integer, Long> specRscTotal = resourceService.getStatisticsForMine(false);

        //map to list
        List<Long> courseMediaTotal = Lists.newArrayList(courseRscTotal.get(MediaType.AUDIO), courseRscTotal.get(MediaType.VEDIO),
                courseRscTotal.get(MediaType.PICTURE), courseRscTotal.get(MediaType.TEXT));
        List<Long> specMediaTotal = Lists.newArrayList(specRscTotal.get(MediaType.AUDIO), specRscTotal.get(MediaType.VEDIO),
                specRscTotal.get(MediaType.PICTURE), specRscTotal.get(MediaType.TEXT));

        Map<String, Object> result = Maps.newHashMap();
        result.put("lessonDetail", courseMediaTotal);
        result.put("majorDetail", specMediaTotal);

        log.debug("return 我的资源统计");
        return new JsonResultData<>(result);
    }


    /**
     * 学生默认页数量统计
     *
     * @return JsonResult
     * @author lyj
     * @since 2015-4-2
     */
    @RequestMapping(value = "/studentDefaultCount")
    @ResponseBody
    public JsonResultData getStudentCount() {
        log.debug("学生默认页数量统计");
        return new JsonResultData(questionService.getCount());
    }

    /**
     * 媒体类型资源统计
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2015-05-05
     */
    @RequestMapping(value = "/myResource", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult myResource(@RequestParam("type") int groupType){
        Assert.isTrue(groupType >= 0,MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));

        //获取统计结果
        Map<String, Object> result = resourceService.getStatisticForMediaByGroup(groupType);

        return new JsonResultData<>(result);
    }

    /**
     * 习题类型习题资源统计
     * @return JsonResult
     *
     * @author fanruiping
     * @since 2015-05-06
     */
    @RequestMapping(value = "/myPraxis", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult myPraxis(){
        //获取统计结果
        Map<String, Object> result = exerciseService.getStatisticsByCreator();

        return new JsonResultData<>(result);
    }
}
